const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");
const tools = require("../../tools/tool");
const {updateInventory} = require("../bill/base");

router.post('/zq/start', async ctx => {
    try {
        let user = ctx.user
        let body = ctx.request.body
        let ym = body.ym;
        let tenantId = user.tenantId;
        let uid = user.id;
        let ctime = tools.getTime()
        await db.transaction(async () => {
            await db.models['zq'].destroy({
                where: {
                    tenantId
                }
            }, )
            await db.models['zq'].create({
                tenantId,
                isCurrent: 1,
                ym,
                ctime,
                cby: uid,
            })
            await updateInventory(tenantId, 0,'init')
        })
        return ctx.suc('已开启账套');
    } catch (e) {
        return ctx.err(e)
    }
})

module.exports = router;
